<?php
/**
 * Created by PhpStorm.
 * User: xuic
 * Date: 2016/7/3
 * Time: 9:48
 */

namespace admin;

use C\lib\Db;
use C\lib\Sql;
use C\mod\user;
use C\mod\orderPiece;
use C\mod\com;

class report extends ctr {

    const listRows = 30;   // 每页显示的几条记录

    // --- 报表/订单 ---

    public function order() {

        global $_STATE_PIECE, $_STATE_IN_PIECE , $_PIECE_TYPE_PAY;

        if($worker = $this->getLogAds()) {

            $this->render('report/order', [
                '_STATE_PIECE' => $_STATE_PIECE,
                '_STATE_IN_PIECE' => $_STATE_IN_PIECE,
                '_PIECE_TYPE_PAY' => $_PIECE_TYPE_PAY
            ]);
        }else
            $this->render('user1/main',['err'=>'请先登陆']);

    }



    // --- 订单 pull ---

    public function orderPull() {

        global $_PIECE_TYPE_PRODUCT, $_PIECE_TYPE_PAY, $_STATE_PIECE, $_STATE_IN_PIECE;

        if($worker = $this->getLogAds()) {

            $page = isset($_POST['page']) && is_numeric($_POST['page']) ? (intval($_POST['page'])-1) * self::listRows : 0 ;

            $where[] = isset($_POST['orderCode']) && trim($_POST['orderCode'])!='' ? ['order_id' , '=' , trim($_POST['orderCode'])] : '';
            $where[] = isset($_POST['pieceCode']) && trim($_POST['pieceCode'])!='' ? ['piece_code' , '=' , trim($_POST['pieceCode'])] : '';
            $where[] = isset($_POST['sendName']) && trim($_POST['sendName'])!='' ? ['name_sender' , 'like' , '%'.$_POST['sendName'].'%'] : '';
            $where[] = isset($_POST['sendAddr']) && trim($_POST['sendAddr'])!='' ? ['address_sender' , 'like' , '%'.$_POST['sendAddr'].'%'] : '';
            $where[] = isset($_POST['sendPhone']) && trim($_POST['sendPhone'])!='' ? ['phone_sender' , '=' , $_POST['sendPhone']] : '';

            $where[] = isset($_POST['sendTime']) && trim($_POST['sendTime'])!='' ? ['time_add' , '>=' , strtotime($_POST['sendTime'])] : '';
            $where[] = isset($_POST['sendTime2']) && trim($_POST['sendTime2'])!='' ? ['time_add' , '<=' , strtotime($_POST['sendTime2'].' 23:59:59')] : '';

            $where[] = isset($_POST['receiveName']) && trim($_POST['receiveName'])!='' ? ['name_recipient' , 'like' , '%'.$_POST['receiveName'].'%'] : '';
            $where[] = isset($_POST['receiveAddr']) && trim($_POST['receiveAddr'])!='' ? ['address_recipient' ,  'like' , '%'.$_POST['receiveAddr'].'%'] : '';
            $where[] = isset($_POST['receivePhone']) && trim($_POST['receivePhone'])!='' ? ['phone_recipient' , '=' , $_POST['receivePhone']] : '';

            $where[] = isset($_POST['tAccount']) && trim($_POST['tAccount'])!='' ? ['user_id' , '=' , $_POST['tAccount']] : '';

            $where[] = isset($_POST['tPayType']) && trim($_POST['tPayType'])!='' ? ['type_pay' , '=' , $_POST['tPayType']] : '';
            $where[] = isset($_POST['tPound']) && trim($_POST['tPound'])!='' ? ['price_goods' , '=' , $_POST['tPound']] : '';
            $where[] = isset($_POST['tPieceState']) && trim($_POST['tPieceState'])!='' ? ['state_in' , '=' , $_POST['tPieceState']] : '';


            $where=array_filter ($where);



            if(isset($_POST['count']) && trim($_POST['count'])!='') {
                $count= $_POST['count']+0;
            } else {
                $sql = new Sql();
                $sql->select('count(id) as count', 'order_piece')->where($where)->by('id', 'DESC');
                $count = ($res = Db::query($sql->sql)) ? $count = $res->fetch(\PDO::FETCH_ASSOC)['count'] : 0;
            }

            // --- 判断分页 ---
            $page = isset($_POST['page']) && trim($_POST['page']) != '' ? intval($_POST['page']) :  1;

            \C\lib\Page::setParam($count , self::listRows , $page ,10);
            \C\lib\Page::setConfig('prev' , '<<');
            \C\lib\Page::setConfig('next' , '>>');
            \C\lib\Page::setConfig('first', '首页');
            \C\lib\Page::setConfig('last' , '尾页');
            \C\lib\Page::setConfig('theme', "共 %TOTAL_ROW% 条记录( %NOW_PAGE% / %TOTAL_PAGE% 页) %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");


            $jPieces = [];
            $jUsers = [
                '0' => [
                    'phone' => '测试账户'
                ]
            ];
            $limit1 = ( $page-1) * self::listRows;
            $pieces = orderPiece::getList($where, [$limit1 ,self::listRows], ['id', 'desc']);


            $userIds = [];
            foreach ($pieces as $piece) {
                $jPieces[] = [
                    'id' => $piece->id,
                    'pieceCode' => $piece->piece_code,
                    'orderCode' => $piece->order_id,
                    'address_sender' => $piece->address_sender,
                    'phone_sender' => $piece->phone_sender,
                    'name_sender' => $piece->name_sender,
                    'address_recipient' => $piece->address_recipient,
                    'phone_recipient' => $piece->phone_recipient,
                    'name_recipient' => $piece->name_recipient,
                    'time_add' => date('Y-m-d H:i:s', $piece->time_add),
                    'price_goods' => $piece->price_goods,
                    'price_pop' => $piece->price_pop,
                    'price' => $piece->price,
                    'type_pay' => $_PIECE_TYPE_PAY[$piece->type_pay],
                    'type_product' => $_PIECE_TYPE_PRODUCT[$piece->type_product],
                    'state_pay' => $piece->state_pay,
                    'state' => $piece->state,
                    'state_name' => $_STATE_PIECE[$piece->state],
                    'user_id' => $piece->user_id,
                    'state_in' => $piece->state_in,
                    'state_in_name' => $_STATE_IN_PIECE[$piece->state_in],
                    'state_in' => $piece->state_in,
                    'comId'=>$piece->com_id
                ];
                $userIds[] = $piece->user_id;
            }
            $userIds = array_unique($userIds);


            $jUsers = [];
            if(count($userIds)) {
                $users = user::getList([['id', 'in', $userIds]]);
                foreach ($users as $user) {
                    $jUsers[$user->id] = [
                        'phone' => $user->phone
                    ];
                }
            }

            $arr = array_unique(array_column($jPieces , 'comId'));

            $jCom[0] = '';
            if(count($arr)>1) {
                $coms  = com::getList([['id','in' , $arr]]);
                foreach ($coms as $com) {
                    $jCom[$com->id] = $com->name;
                }
            }




            $jUsers[0] = ['无电话'];
            $this->writeJson(1, [
                'pieces' => $jPieces,
                'users' => $jUsers,
                'count' => $count,
                'com' => $jCom,
                'pages' => \C\lib\Page::show()
            ]);
        }

    }

    public function statisticalTable() {
        if($worker = $this->getLogAds()) {
            if(ctr::canStatisticalTable()) {

                $stmt = Db::query('SELECT city_id , cdc.name city_name  FROM cd_worker cdw, cd_city  cdc where cdw.city_id=cdc.id  GROUP BY city_id');
                $this->render('report/statisticalTable');
            }else
                $this->render('user1/main',['err'=>'无此操作权限']);
        }else
            $this->writeJson( 0 , '请先登陆' ,1 );
    }

    public function getStaticstical() {
        if($worker = $this->getLogAds()) {
            if(ctr::canStatisticalTable()) {
                $stmt = Db::query('SELECT FROM_UNIXTIME(time_add, \'%Y-%m\') months,COUNT(id) counts FROM cd_order_piece  GROUP BY months');
                $months = []; $counts = [];
                if($stmt && $stmt->rowCount()) {
                     while($d = $stmt->fetch(\PDO::FETCH_ASSOC)) {
                         $months[] = $d['months'];
                         $counts[] = $d['counts'];
                     }
                }
                $this->writeJson( 1 , ['jdatas'=> ['months'=>$months , 'counts'=>$counts]] , 1);
            }else
                $this->render('user1/main',['err'=>'无此操作权限']);
        }else
            $this->writeJson( 0 , '请先登陆' ,1 );
    }

}